package at.ac.tuwien.ifs.calendit.basis.gui;

import java.awt.Color;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.sql.Date;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JTable;
import javax.swing.SwingUtilities;
import javax.swing.table.TableColumnModel;

import at.ac.tuwien.ifs.calendit.domain.Termin;

class JWochenAnsichtMouseListener implements MouseListener {
	  private JTable __table;
	  public static Color c;
	  public static int lastrow=0;
	  public static int lastcolumn=0;
	  private void __forwardEventToButton(MouseEvent e) {
	    TableColumnModel columnModel = __table.getColumnModel();
	    int column = columnModel.getColumnIndexAtX(e.getX());
	    int row    = e.getY() / __table.getRowHeight();
	    Object value;
	    JLabel button;
	    MouseEvent buttonEvent;
	    

	    if(row >= __table.getRowCount() || row < 0 ||
	       column >= __table.getColumnCount() || column < 0)
	      return;

	    value = __table.getValueAt(row, column);

	    if(!(value instanceof JLabel))
	      return;

	    button = (JLabel)value;
	    

	    buttonEvent =
	      (MouseEvent)SwingUtilities.convertMouseEvent(__table, e, button);
	    button.dispatchEvent(buttonEvent);
	    // This is necessary so that when a button is pressed and released
	    // it gets rendered properly.  Otherwise, the button may still appear
	    // pressed down when it has been released.
	    __table.repaint();
	  }

	  public JWochenAnsichtMouseListener(JTable table) {
	    __table = table;
	  }

	  public void mouseClicked(MouseEvent e) {
		  int row= __table.getSelectedRow();
		  int column= __table.getSelectedColumn();
		  if (e.getClickCount() >= 2) {
			 
			 
			 if(JWochenAnsichtTableModel.__rows[row][column].getClass().toString().equals("class javax.swing.JButton")){
				 Termin t=JWochenAnsichtTableModel.getTerminAt(row, column);
				 
				 KalenderUebersicht.editNewTermin(t);
				 
				 //TODO: Fehlermeldung bei edit beheben
				
			  }
			  else{
				 Date d = new java.sql.Date(Wochenansicht.cal.getDate().getYear(), Wochenansicht.cal.getDate().getMonth(), Wochenansicht.cal.getDate().getDay());
				 Termin t = new Termin();
				 t.setBeginnDatum(d);
				 t=JWochenAnsichtTableModel.getTerminAt(row, column);

				  KalenderUebersicht.createNewTermin(t); 
			  }
		  }
		  
	    __forwardEventToButton(e);
	  }

	  public void mouseEntered(MouseEvent e) {
	    __forwardEventToButton(e);
	  }

	  public void mouseExited(MouseEvent e) {
	    __forwardEventToButton(e);
	  }

	  public void mousePressed(MouseEvent e) {
		  int row= __table.getSelectedRow();
			 int column=__table.getSelectedColumn();
			 
				  if(JWochenAnsichtTableModel.__rows[row][column].getClass().toString().equals("class javax.swing.JButton")){
				 //Label: NEW, Button: EDIT
				 
					 if(lastrow!=0 || lastcolumn!=0){
						 JWochenAnsichtTableModel.btn[lastrow][lastcolumn-1].setBackground(c);
						 JWochenAnsichtTableModel.__rows[lastrow][lastcolumn]=JWochenAnsichtTableModel.btn[lastrow][lastcolumn-1];
					 }
				 
				 c= JWochenAnsichtTableModel.btn[row][column-1].getBackground();
				 ((JButton)JWochenAnsichtTableModel.btn[row][column-1]).setBackground(Color.green);
				 	JWochenAnsichtTableModel.__rows[row][column]=JWochenAnsichtTableModel.btn[row][column-1];
					lastrow=row;
					lastcolumn=column;
				}

				 
	    __forwardEventToButton(e);
	  }

	  public void mouseReleased(MouseEvent e) {
	    __forwardEventToButton(e);
	  }
	}

	

